package beans;

import java.util.List;

import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.PersistenceContextType;
import javax.persistence.Query;

import data.User;

/**
 * Session Bean implementation class UserActions
 */
@Stateless
@LocalBean
public class UserActions {

	@PersistenceContext(unitName = "EJB_Persistance", type = PersistenceContextType.EXTENDED)
    EntityManager em;
	
    /**
     * Default constructor. 
     */
    public UserActions() {
        // TODO Auto-generated constructor stub
    }
    
    
    public boolean loginAction(String userID, String password) {

    	Query query_searchUser = em.createNativeQuery(
    			"select * from user where user_id='" + userID + "'", User.class);
    	
    	
    	List<User> res_searchUser = query_searchUser.getResultList();
    	
    	if(res_searchUser.size() == 0){
    		return false;
    	}
    	
    	User user = res_searchUser.get(0);
    	if(password==null || !password.equals(user.getPassword())){
    		return false;
    	}
    		
    	
    	//TODO create new record in access table on DB
    	
    	return true;
	}

    public void logoutAction() {
		// TODO Auto-generated method stub

	}
    
    public void createUserAction() {
		// TODO Auto-generated method stub

	}


    public void deleteUserAction() {
		// TODO Auto-generated method stub

	}

}
